/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "MouseControl.h"

MouseControl::MouseControl(QObject* parent) : QObject(parent)
{
    m_gesture = DragNone;
    m_dragState = DragNone;
    m_timer.setSingleShot(true);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(emitEevent()));
}

void MouseControl::attatchWidget(QWidget* widget)
{
    connect(this, SIGNAL(sendGestureEvent(GestureEvent*)), widget, SLOT(mouseCtlEvent(GestureEvent*)));
}

void MouseControl::mousePressEvent(QMouseEvent* event)
{
    if (m_timer.isActive())
    {
        m_timer.stop();
        return;
    }

    if (event->button() == Qt::LeftButton)
    {
        m_dragCount = 0; 
        m_gesture = DragNone;
        m_dragState = DragNone;
        m_startPos = event->pos();
        m_lastPos = event->pos();
    }
}

void MouseControl::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        if (event->pos() == m_lastPos) return;
        int dx = event->pos().x() - m_lastPos.x();
        int adx = abs(dx);
        int dy = event->pos().y() - m_lastPos.y();
        int ady = abs(dy);
        m_lastPos = event->pos();
        int dragCount;
        DragType dragType;
        if (adx > ady)
        {
            dragCount = adx;
            if (dx > 0)
            {
                dragType = DragRight; 
            }
            else
            {
                dragType = DragLeft;
            }
        }
        else
        {
            dragCount = ady;
            if (dy > 0)
            {
                dragType = DragDown; 
            }
            else
            {
                dragType = DragUp;
            }
        }

        if (m_dragState != dragType)
        {
            m_dragCount = 0; 
            m_dragState = dragType;
        }
        if (m_dragCount >= DRAG_THRESH) return;
        m_dragCount += dragCount;
        if (m_dragCount >= DRAG_THRESH && LAST_DRAG_ACT(m_gesture) != dragType)
        {
            ADD_DRAG_ACT(m_gesture, dragType); 
        }
    }
}

void MouseControl::mouseReleaseEvent(QMouseEvent* event)
{
    m_releasePos = event->pos();
    m_timer.start(50);
}

void MouseControl::emitEevent()
{
    if (m_gesture == DragNone)
    {
        m_gesture = DragClick; 
    }

    m_gestureEvent.gesture = m_gesture; 
    m_gestureEvent.clickPos = m_startPos;
    m_gestureEvent.offset = m_releasePos - m_startPos;
    emit sendGestureEvent(&m_gestureEvent);
}

